package view.list_panel;

import java.util.Vector;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import listener.ListChangeEvent;
import listener.ListChangeListener;
import listener.TimeChangeEvent;
import listener.TimeChangeListener;
import model.CurrentParticipant;
import model.Debate;
import model.Participant;

public class ParticipantList extends JList implements ListSelectionListener, ListChangeListener, TimeChangeListener {

	private static final long serialVersionUID = -2858444583121076L;
	
	private Vector<Participant> participanList;
	private CurrentParticipant part;

	public ParticipantList(Debate debate) {
		part = debate.getCurrentParticipant();
		participanList = debate.getParticipants();
		addListSelectionListener(this);
		debate.getParticipants().addListChangeListener(this);
	}

	public void valueChanged(ListSelectionEvent e) {
		part.setParticipant(participanList.elementAt(getSelectedIndex()));
		part.getParticipant().getClock().getMinute().addTimeChangeListener(this);
		part.getParticipant().getClock().getSecond().addTimeChangeListener(this);
	}

	public void listChanged(ListChangeEvent e) {
		if (!e.getSource().isEmpty()) {
			removeListSelectionListener(this);
			if (!e.getSource().hasIncreased()) 
				setSelectedIndex(-1);
			setListData(e.getSource());
			addListSelectionListener(this);
		}
		
		repaint();
		validate();
	}

	public void timeChange(TimeChangeEvent e) { 
		repaint();
		validate();
	}
}
